package com.ruoyi.system.service.impl.vip;

import java.math.BigDecimal;
import java.time.Year;
import java.util.List;

import com.ruoyi.common.core.domain.entity.TotalResultMap;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.TimeStampUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.vip.VsDramasVip;
import com.ruoyi.system.mapper.vip.VsDramasVipMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.vip.VsDramasVipOrderMapper;
import com.ruoyi.system.domain.vip.VsDramasVipOrder;
import com.ruoyi.system.service.vip.IVsDramasVipOrderService;

/**
 * VIP订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-25
 */
@Service
public class VsDramasVipOrderServiceImpl implements IVsDramasVipOrderService 
{
    @Autowired
    private VsDramasVipOrderMapper vsDramasVipOrderMapper;

    @Autowired
    private VsDramasVipMapper vsDramasVipMapper;

    /**
     * 查询VIP订单
     * 
     * @param id VIP订单主键
     * @return VIP订单
     */
    @Override
    public VsDramasVipOrder selectVsDramasVipOrderById(Integer id)
    {
        return vsDramasVipOrderMapper.selectVsDramasVipOrderById(id);
    }

    /**
     * 查询VIP订单列表
     * 
     * @param vsDramasVipOrder VIP订单
     * @return VIP订单
     */
    @Override
    public List<VsDramasVipOrder> selectVsDramasVipOrderList(VsDramasVipOrder vsDramasVipOrder)
    {
        return vsDramasVipOrderMapper.selectVsDramasVipOrderList(vsDramasVipOrder);
    }

    /**
     * 新增VIP订单
     * 
     * @param vsDramasVipOrder VIP订单
     * @return 结果
     */
    @Override
    public String insertVsDramasVipOrder(VsDramasVipOrder vsDramasVipOrder)
    {
        VsDramasVip vsDramasVip = vsDramasVipMapper.selectVsDramasVipById(vsDramasVipOrder.getId());
        VsDramasVipOrder order = new VsDramasVipOrder();
        order.setVipId(vsDramasVip.getId());
        String s = "A" + System.currentTimeMillis() + IdUtils.fastSimpleUUID();
        order.setOrderSn(s);
        order.setLangId(vsDramasVipOrder.getLangId());
        Long id = SecurityUtils.getLoginUser().getVsUser().getId();
        order.setUserId(Math.toIntExact(id));
        order.setStatus(0L);
        order.setTotalFee(vsDramasVip.getPrice());
        order.setPayFee(BigDecimal.valueOf(0));
        if ("周".equals(vsDramasVip.getType())) {
            int num = Integer.parseInt(vsDramasVip.getNum());
            int result = 7 * num;
            long timeStampByDate = TimeStampUtils.getTimeStampByDate(result);
            order.setTimes(timeStampByDate);
        } else if ("月".equals(vsDramasVip.getType())) {
            int num = Integer.parseInt(vsDramasVip.getNum());
            int result = 30 * num;
            long timeStampByDate = TimeStampUtils.getTimeStampByDate(result);
            order.setTimes(timeStampByDate);
        } else if ("年".equals(vsDramasVip.getType())) {
            int num = Integer.parseInt(vsDramasVip.getNum());
            // 获取当前年份
            int currentYear = Year.now().getValue();
            // 由于每年的天数取决于是否是闰年，可以通过以下方式计算
            boolean isLeapYear = Year.isLeap(currentYear);
            int daysInYear = isLeapYear ? 366 : 365;
            int result = daysInYear * num;
            long timeStampByDate = TimeStampUtils.getTimeStampByDate(result);
            order.setTimes(timeStampByDate);
        }

        order.setCreatetime(System.currentTimeMillis());
        order.setUpdatetime(System.currentTimeMillis());
        int i = vsDramasVipOrderMapper.insertVsDramasVipOrder(order);
        if (i > 0) {
            return s;
        }
        return "err";
    }

    /**
     * 修改VIP订单
     * 
     * @param vsDramasVipOrder VIP订单
     * @return 结果
     */
    @Override
    public int updateVsDramasVipOrder(VsDramasVipOrder vsDramasVipOrder)
    {
        vsDramasVipOrder.setUpdatetime(System.currentTimeMillis());
        return vsDramasVipOrderMapper.updateVsDramasVipOrder(vsDramasVipOrder);
    }

    /**
     * 批量删除VIP订单
     * 
     * @param ids 需要删除的VIP订单主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasVipOrderByIds(Integer[] ids)
    {
        return vsDramasVipOrderMapper.deleteVsDramasVipOrderByIds(ids);
    }

    /**
     * 删除VIP订单信息
     * 
     * @param id VIP订单主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasVipOrderById(Integer id)
    {
        return vsDramasVipOrderMapper.deleteVsDramasVipOrderById(id);
    }

    /**
     * 查询订单统计信息
     *
     * @param vsDramasVipOrder VIP订单
     * @return 结果
     */
    @Override
    public TotalResultMap selectCount(VsDramasVipOrder vsDramasVipOrder) {
        return vsDramasVipOrderMapper.selectCount(vsDramasVipOrder);
    }
}
